<template>
	<view>
		<uv-empty mode="coupon" text="暂无卡券" iconColor="#d0021b" textColor="black" marginTop="32"
			v-show="noDataFlag"></uv-empty>
		<view class="padding-lr32">
			<view class="coupon" v-for="record in coupons">
				<view class="coupon-value">{{formatCouponValue(record)}}</view>
				<view class="coupon-type-time">
					<view class="coupon-type">{{record.typeName}}</view>
					<view class="coupon-desc">限投资项目使用</view>
					<view class="coupon-time">有效期至{{record.deadline}}</view>
				</view>
				<view class="coupon-action" v-show="showActionFlag">
					<uv-button type="error" text="去使用" size="small" @click="selectCoupon(record)"></uv-button>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				coupons: [],
				noDataFlag: false,
				showActionFlag: false,
				type: '',
			}
		},
		onLoad(option) {
			var that = this;
			that.type = option.type;
			if (that.type) {
				that.showActionFlag = true;
			}
			that.findActiveCoupon();
		},
		methods: {

			selectCoupon(record) {
				uni.navigateBack({
					success: function() {
						uni.$emit('returnSelectedCoupon', JSON.parse(JSON.stringify(record)));
					}
				});
			},

			formatCouponValue(record) {
				if (record.type == '1') {
					return 'USDT' + record.cashValue;
				}
				return record.additionalRate + '%';
			},

			findActiveCoupon() {
				var that = this;
				uni.$uv.http.get('/coupon/findActiveCoupon', {
					params: {
						type: that.type
					}
				}).then(res => {
					that.coupons = res.data;
					that.noDataFlag = that.coupons.length == 0;
				});
			},
		}
	}
</script>

<style>
	.coupon {
		display: flex;
		align-items: center;
		background: linear-gradient(45deg, #bec4c8, #94c2e7);
		border-radius: 16rpx;
		padding-left: 32rpx;
		padding-right: 32rpx;
		padding-top: 12rpx;
		padding-bottom: 12rpx;
		margin-bottom: 32rpx;
	}

	.coupon-value {
		color: red;
		font-weight: bold;
		font-size: larger;
		text-align: center;
		flex: 1;
	}

	.coupon-type-time {
		flex: 2;
	}

	.coupon-desc {
		color: #555454;
		font-size: small;
		padding-bottom: 12rpx;
	}

	.coupon-type {
		font-weight: bold;
		padding-bottom: 12rpx;
	}

	.coupon-time {
		color: #555454;
		font-size: small;
	}
</style>